package com.example.litterpainterdemo.views

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.example.litterpainterdemo.R
import com.example.litterpainterdemo.utils.OrientationType

class IndicatorView(context: Context, attrs: AttributeSet?): View(context, attrs) {
    private var mSelectDot = dp2px(40)//选中点的宽度
    private var mNormalDot = dp2px(20)//默认点的宽度
    private var mSpace = dp2px(8)//两点之间的间距
    private var mDotHeight = dp2px(12)//点的高度
    private var mCornerRadius = dp2px(6)//圆角半径
    private var mStartX = 0f
    private var top = 0f //顶部
    private var selectColor = resources.getColor(R.color.main_red,null)
    private var normalColor = resources.getColor(R.color.light_dark,null)
    private var _currentDot = 0 //记录当前选中的点
    private var type = OrientationType.NONE//记录方向
    private var mMoveSpace = 0f //动画时宽度的变化值0-20

    //画笔
    private val dotPaint:Paint by lazy {
        Paint().apply {
            style = Paint.Style.FILL
            color = normalColor
            isAntiAlias = true//抗锯齿
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        var widthSize = MeasureSpec.getSize(widthMeasureSpec)

        when(widthMode){
            MeasureSpec.EXACTLY -> {}
            else -> widthSize = (mSelectDot+3*mNormalDot+5*mSpace).toInt()
        }

        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        var heightSize = MeasureSpec.getSize(heightMeasureSpec)

        when(heightMode){
            MeasureSpec.EXACTLY -> {}
            else -> heightSize = (mDotHeight+2*mSpace).toInt()
        }

        setMeasuredDimension(widthSize,heightSize)
    }


    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mStartX = (width-(mSelectDot+3*mNormalDot+3*mSpace))/2
        top = (height - mDotHeight)/2
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        for (i in 0 until 4){
            var left = mStartX + if(i > _currentDot){
                mSelectDot+i*mSpace+(i-1)*mNormalDot
            }else{
                i*(mSpace+mNormalDot)
            }

            var right = left + if(i == _currentDot) mSelectDot else mNormalDot


            /**
             * 往左 当前改变right  下一个改变left
             * 往右 当前改变left 上一个改变right
             */
            if(type != OrientationType.NONE){
                when(i){
                    _currentDot -> {//当前点
                        if(type == OrientationType.MOVE_LEFT){
                            right -= dp2px(20)
                            right += mMoveSpace
                        }else{
                            left  += dp2px(20)
                            left -= mMoveSpace
                        }
                    }
                    _currentDot -1 -> {//上一个点
                        if(type == OrientationType.MOVE_RIGHT){
                            right += dp2px(20)
                            right -= mMoveSpace
                        }
                    }
                    _currentDot +1 -> {//下一个点
                        if(type == OrientationType.MOVE_LEFT){
                            left  -= dp2px(20)
                            left += mMoveSpace
                        }
                    }
                }
            }

            if(i == _currentDot) dotPaint.color = selectColor else dotPaint.color = normalColor
            canvas?.drawRoundRect(left,top,right,top+mDotHeight,mCornerRadius,mCornerRadius,dotPaint)


        }

    }

    //使用动画产生一系列的值
    private fun startValueAnim(){
        ValueAnimator.ofFloat(0f,dp2px(20)).apply {
            duration = 500
            addUpdateListener {
                mMoveSpace = it.animatedValue as Float
                invalidate()
            }
            start()
        }
    }

    //改变选中点
    fun changeSelect(currentDot:Int){
        if(currentDot  == _currentDot) {
            type = OrientationType.NONE
            return
        }

        //判断方向
        type = if(currentDot > _currentDot){
            OrientationType.MOVE_RIGHT
        }else{
            OrientationType.MOVE_LEFT
        }

        _currentDot = currentDot
        startValueAnim()
    }


    //dp2px
    private fun dp2px(dp:Int) = resources.displayMetrics.density*dp
}